package com.shenhangye.pushlive.camera.view

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.ViewGroup
import com.shenhangye.pushlive.camera.Camera2FrameCallback
import com.shenhangye.pushlive.camera.Camera2Wrapper
import com.shenhangye.pushlive.camera.CameraUtil.getFitInScreenSize

class CameraSurfaceView:SurfaceView, SurfaceHolder.Callback, Camera2FrameCallback {

    var mCamera2Wapper:Camera2Wrapper?=null
    constructor(context: Context,att:AttributeSet):super(context,att){

         holder.addCallback(this)

        mCamera2Wapper=Camera2Wrapper(context,this)

    }

    private  val TAG = "CameraSurfaceView"
//    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
//        val width = MeasureSpec.getSize(widthMeasureSpec)
//        val height = MeasureSpec.getSize(heightMeasureSpec)
//        if (aspectRatio == 0f) {
//        setMeasuredDimension(width, height)
//        } else {
//            Log.d(TAG, "Measured dimensionsc old set: $width x $height")
//            // Performs center-crop transformation of the camera frames
//            val newWidth: Int
//            val newHeight: Int
//            Log.d(TAG, "aspectRatio: $aspectRatio ")
//            val actualRatio = if (width > height) aspectRatio else 1f / aspectRatio
//            if (width < height * actualRatio) {
//                newHeight = height
//                newWidth = (height * actualRatio).roundToInt()
//            } else {
//                newWidth = width
//                newHeight = (width / actualRatio).roundToInt()
//            }
//
//            Log.d(TAG, "Measured dimensions set: $newWidth x $newHeight")
//            setMeasuredDimension(newWidth, newHeight)
//        }
//    }

    override fun surfaceCreated(holder: SurfaceHolder) {
//      mCamera2Wapper?.characteristics?.let {
//            getPreviewOutputSize(
//                display,
//                it,
//                SurfaceHolder::class.java
//            )
//        }.apply {
//            Log.e(TAG, "$width---$height")
////            setAspectRatio(width,height)
//
//
//        }

        val displayMetrics = resources.displayMetrics;
        val widthScreen = displayMetrics.widthPixels;
        val heightScreen = displayMetrics.heightPixels;
        Log.e(TAG, "$widthScreen--surfaceCreated1-$heightScreen")
        mCamera2Wapper?.previewSize?.let {
            Log.e(TAG,"previewSize"+it.width+"---"+it.height)
            getFitInScreenSize(it.width,it.height,widthScreen,heightScreen).apply {
                Log.e(TAG, "$width--surfaceCreated-$height")
//                setAspectRatio(width,height)

                val lp: ViewGroup.LayoutParams = layoutParams
                lp.width = width // required width
                lp.height = height // required height
                setLayoutParams(lp)
            }
        }


        mCamera2Wapper?.startCamera(holder.surface)
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {

    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {

    }

    override fun onPreviewFrame(data: ByteArray?, width: Int, height: Int) {

    }

    override fun onCaptureFrame(data: ByteArray?, width: Int, height: Int) {

    }

    private var aspectRatio = 0f
    fun setAspectRatio(width: Int, height: Int) {
        require(width > 0 && height > 0) { "Size cannot be negative" }
        Log.e(TAG,"setAspectRatio")
        aspectRatio = width.toFloat() / height.toFloat()
        holder.setFixedSize(width, height)
        requestLayout()
    }

}